C (176/301)

From:Ben Hutchings
Date:29 Aug 99 at 00:58:35
Subject:Re: Re: ExecBase & 'RESET:starting PC' exception

From: Ben Hutchings <womble@zzumbouk.demon.co.uk>

On Sat, Aug 28, 1999 at 06:42:45PM +0100, Andrew Markwell wrote:
> From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
>
> On 28-Aug-99 did write:
> > From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
> >
> > On Fri, Aug 27, 1999 at 08:07:59PM -0600, Laura Vance wrote:
> >> From: Laura Vance <vancel@amiga.nols.com>
> >>
> >> On Sat, 28 Aug 1999 01:30:57 +0100,
> >> Ben Hutchings <womble@zzumbouk.demon.co.uk> wrote about Re: [amiga-c] Re:
> >> ExecBase & 'RESET:starting PC' exception:
> >>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
> >>>
> >>> On Sat, Aug 28, 1999 at 12:44:33AM +0100, Andrew Markwell wrote:
> >>>> From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
> >>>>
> >>>> On 27-Aug-99 did write:
> >>>>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
> >>>>>
> >>>>> On Fri, Aug 27, 1999 at 12:42:39AM +0100, Andrew Markwell wrote:
> >>>>>> From: Andrew Markwell <andrewmarkwell@ukonline.co.uk>
> >>>>>>
> >>>>>> On 27-Aug-99 did write:
> >>>>>> From: Ben Hutchings <womble@zzumbouk.demon.co.uk>
> >>>
> >>> <snip>
> >>>>>>> You should use const char * here, not char *.
> >>>>>>>
> >>>>>> er, why?
> >>>>>
> >>>>> String literals are arrays of const char.
> >>>>>
> >>>> First I've heard of it.
> >>>
> >>> You doubt what I say? Perhaps you should find some decent documentation
> >>> on standard C or C++.
>
> There is no mention of such use of const in Herbert Schildt's, Ira
> Pohl's, Dave Kelly's or Adrian Robson's (my own lecturer) books.

You may be using the wrong books, then. You could even try reading
standards documents.

> >>> For hysterical raisins, you are allowed to assign string literals to
> >>> objects of type char *. That's just a special case; it doesn't mean that
> >>> they are really arrays of char.
> >>>
> >>
> >> I know that I'm sticking my nose in a conversation where it's not welcome,
> >> but it seems to me that a char * is a pointer to the beginning of a null
> >> terminated array of type char.
> >
> > char * is a pointer to char; it may point to just one char or to the
> > first element of an array of char, which may or may not be null-
> > terminated.
>
> It is considered bad practice to use an array of char's for anything
> other than a character array representing a string.

You may think so, but I disagree. Sometimes a character array is not
meant to be accessed sequentially. Sometimes it has a length count at
the beginning. Null terminated strings are just a convention that the
compiler and standard library provide some support for.

> If you really must, then use a short int instead.

No thanks.

> >> In the C and C++ classes that I've taken in college, they say to
> >> reserve the const char * for something that will definately not
> >> change.
> >
> > That's not quite right. You use pointers of type const char * where
> > you do not need to modify the characters pointed to. The characters
> > being pointed to might not really be constant, and may be modifiable
> > from other parts of the program.
>
> Laura is right. The C++ 'string' class is now the standard for
> using strings in C++.

No, it's one thing you can use.

> And using 'const' in that fashion will only prevent the address to
> the base of the character array from changing and not sequential
> characters.

Wrong. You are thinking of char * const.

> There is in fact no way of preventing changes to such a data
> structure.

No.

> If you wish a string to be made constant then use the C++ string
> class with const.

No.

> >> And since we only saw a code fragment, we do not know that other
> >> string literals will not be passed into the error handling function.
> >
> > Of course they may be, but that has no bearing on their type.
> >
>
> I could easily have written:
>
> catch (string error)
> {
>
> }
>
> instead. Then it doesn't matter whether you:
>
> throw "error";
>
> or
>
> string x="error";
> throw x;
>
> or
>
> char x[]="error";
> throw x;

You've clearly never actually tried this.



Ben Hutchings - womble@zzumbouk.demon.co.uk, http://www.zzumbouk.demon.co.uk
Team *AMIGA* | Jay Miner Society | Linux - the choice of a GNU generation
Gates has joked that everything goes on and off unexepectedly in the house,
which is run by a high-end PC network built on Windows NT. - Seattle Times